package cn.edu.buaa.nlsde.grid.DatabaseManage;

import java.sql.*;
import java.util.*;
import cn.edu.buaa.nlsde.grid.DatabaseConnect.*;
import cn.edu.buaa.nlsde.grid.Structure.*;

public class GlobalMapping
{
	DBConn db=null;
	DBPool dbpool=null;
	Statement stat=null;
	ResultSet rs=null;
	String sql="";
	
	public GlobalMapping(DBConn db)
	{
		this.db=db;
		try
		{			
			stat=db.connect();
			sql="create table if not exists GlobalMapping (GridID int(11),ClusterID int(11),HostID int(11),PRIMARY KEY(GridID,ClusterID,HostID))";
			stat.executeUpdate(sql);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	public GlobalMapping(DBPool dbpool)
	{
		this.dbpool=dbpool;
		try
		{			
			stat=dbpool.connect();
			sql="create table if not exists GlobalMapping (GridID int(11),ClusterID int(11),HostID int(11),PRIMARY KEY(GridID,ClusterID,HostID))";
			stat.executeUpdate(sql);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void buildMap(int GridID,int ClusterID,int HostID)
	{
		try
		{	
			stat=dbpool.connect();
			sql="insert into GlobalMapping (GridID,ClusterID,HostID) values ("+GridID+","+ClusterID+","+HostID+")";
			System.out.println(sql);
			stat.executeUpdate(sql);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public Grid getGrids()
	{
		try
		{
			stat=dbpool.connect();
			sql="select * from Grid_Config where Grid_Config.GridID IN (select distinct(GlobalMapping.GridID) from GlobalMapping)";
			rs=stat.executeQuery(sql);
			Grid grid=new Grid();
			if(rs.next())
			{
				grid.setID(rs.getInt("GridID"));
				grid.setName(rs.getString("GridName"));
			}
			return grid;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public List<Cluster> getClusters(int GridID)
	{
		try
		{
			stat=dbpool.connect();
			sql="select * from Cluster_Config where Cluster_Config.ClusterID IN (select distinct(GlobalMapping.ClusterID) from GlobalMapping where GridID="+GridID+")";
			rs=stat.executeQuery(sql);
			List<Cluster> list=new ArrayList<Cluster>();
			while (rs.next())
			{
				Cluster cluster=new Cluster();
				cluster.setID(rs.getInt("ClusterID"));
				cluster.setName(rs.getString("ClusterName"));
				list.add(cluster);
			}
			rs.close();
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public List<Host> getHosts(int ClusterID)
	{
		try
		{
			stat=dbpool.connect();
			sql="select * from Host_Config where Host_Config.HostID IN (select distinct(GlobalMapping.HostID) from GlobalMapping where ClusterID="+ClusterID+")";
			rs=stat.executeQuery(sql);
			List<Host> list=new ArrayList<Host>();
			while (rs.next())
			{
				Host host=new Host();
				host.setID(rs.getInt("HostID"));
				host.setName(rs.getString("HostName"));
				list.add(host);
			}
			rs.close();
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public void addHost(Host host)
	{
		
	}
	
	public void close() throws SQLException
	{
		dbpool.close();
	}
}
